package com.android.logger;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.ListView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.android.logcat.LoggerMonitor;
import com.android.logcat.LogUtil;
import com.android.logcat.LoggerConfig;
import com.android.logcat.view.LogViewrListAdapter;
import com.android.logcat.view.LoggerFloatWindow;

/**
 * @author GYL
 * @version 1.0
 * @dateTime 2020/6/30-16:11
 * @description
 */
public class MainActivity extends AppCompatActivity {
  
  LogViewrListAdapter mListAdapter;
  
  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
  
    ListView listView=findViewById(R.id.list);
    mListAdapter=new LogViewrListAdapter(this);
    listView.setAdapter(mListAdapter);
  
    LogUtil.d("test d");
    LogUtil.i("test I");
    LogUtil.v("test V");
    LogUtil.w("test W");
    LogUtil.e("test e");
    LogUtil.wtf("test WTF");
    LogUtil.println("test print");
    String str=null;
     try {
         boolean s=str.equals("");
      }catch (Exception e){
         LogUtil.e("Main","Test Exception",e);
      }
  }
  
  private int i;
  public void onClick(View view) {
    if(view.getId()==R.id.btn){
      LogUtil.d("test d");
      LogUtil.i("test I");
      LogUtil.v("test V");
      LogUtil.w("test W");
      LogUtil.e("test e");
      LogUtil.wtf("test WTF");
      LogUtil.println("test print");
      LoggerConfig.getInstance().execRunnable(new Runnable() {
        @Override
        public void run() {
          do {
            LogUtil.d("log d " + (i++));
            try {
              Thread.sleep(5000);
            } catch (InterruptedException e) {
            }
          } while (true);
        }
      });
    }else if(view.getId()==R.id.btn2){
      mListAdapter.clear();
      Intent showIntent = LoggerConfig.getInstance().getShowStandOutWindowIntent(LoggerFloatWindow.class);
      LoggerConfig.getInstance().bindLogWindowService(showIntent);
    }else if(view.getId()==R.id.btn3){
      LoggerConfig.getInstance().setLogViewMonitor(new LogView());
    }
  }
  
  private Handler mHandler=new Handler(Looper.getMainLooper()){
    @Override
    public void handleMessage(@NonNull Message msg) {
      mListAdapter.add(String.valueOf(((char)msg.arg1)),(String) msg.obj);
      //super.handleMessage(msg);
    }
  };
  
  class LogView implements LoggerMonitor {
  
    @Override
    public void logger(String loggerLevel, String content) {
        Message message=Message.obtain();
        message.arg1=loggerLevel.charAt(0);
        message.obj=content;
        mHandler.sendMessage(message);
    }
  }
}
